package com.bana.sys.service;

import com.bana.sys.dao.DepartmentDAO;
import com.bana.sys.dao.PersonDAO;
import com.bana.sys.dao.UserDAO;
import com.bana.sys.model.Department;
import com.bana.sys.model.TreeNode;
import com.bana.sys.model.User;
import com.bana.sys.utils.Convert;
import com.bana.sys.utils.Token;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentService {

    @Resource
    DepartmentDAO departmentDAO;

    @Resource
    PersonDAO personDAO;

    @Resource
    UserDAO userDAO;

    public Department getDepartmentTree(String token)
    {
        User user=Token.getUser(token);
        if(user==null)
        {
            return null;
        }
        else
        {
            if(user.getJgbm()==Department.None)
            {
                return null;
            }
            Department department=departmentDAO.getDepartment(user.getJgbm());
            if(department==null)
            {
                return null;
            }
            else
            {
                getDepartmentTree(department);
                return department;
            }
        }
    }

    private void getDepartmentTree(Department parent)
    {
        List<Department> departments=departmentDAO.getDepartmentChildren(parent.getJgbm());
        if(departments.isEmpty())
        {
            parent.setChildren(null);
        }
        else
        {
            List<TreeNode> treeNodes=new ArrayList<>();
            for(Department department:departments)
            {
                treeNodes.add(department);
                department.setSjdwsm(parent==null?"":parent.getJgmc());
                getDepartmentTree(department);
            }
            parent.setChildren(treeNodes);
        }

    }

    public String getDepartmentPath(int jgbm)
    {
        Department department=departmentDAO.getDepartment(jgbm);
        List<Integer> jgbms=new ArrayList<>();
        jgbms.add(jgbm);
        getDepartmentPath(department,jgbms);
        return Convert.toString(jgbms,"(",")");
    }

    private void getDepartmentPath(Department department,List<Integer> jgbms)
    {
        List<Department> departments=departmentDAO.getDepartmentChildren(department.getJgbm());
        for(Department child:departments)
        {
            jgbms.add(child.getJgbm());
            getDepartmentPath(child,jgbms);
        }
    }

    @Transactional
    public void deleteDepartment(TreeNode department)
    {
        if(department.getKey()==0)
        {
            return;
        }
        if(department.getChildren()!=null)
        {
            for(TreeNode child:department.getChildren())
            {
                deleteDepartment(child);
            }
        }
        personDAO.updateDepartment(department.getKey());
        userDAO.updateDepartment(department.getKey());
        departmentDAO.deleteDepartment(department.getKey());
    }
}
